<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: lh
 * Time: 2020/07/13
 * Desc: 积分抽奖
 */
class Controller_Pc_Api_V2_Integral_Award extends Controller_Pc_Api_V2_Base
{

    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Integral_Award
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Integral_Award
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('integral_award', $this->request_params);

        // 服务
        $this->service = new Service_Api_Integral_Award();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 积分抽奖数据
    public function action_award()
    {
        try {
            $this->result['data'] = $this->service->award();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 我的中奖记录列表
    public function action_get_award_log()
    {
        try {
            $this->validation->get_award_log()->check();

            $this->result['data']['list'] = $this->service->get_award_log($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }

    // 抽奖
    public function action_luck_draw()
    {
        $db = Database::instance();
        $db->begin();
        try {
            $this->validation->luck_draw()->check();

            $this->result['data'] = $this->service->luck_draw($this->mid, $this->request_params['id']);

            $db->commit();
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());

            $db->rollback();
        }
    }

    // 保存中奖人联系信息
    public function action_save_award_info()
    {
        try {
            $this->validation->save_award_info()->check();

            $this->result['data'] = $this->service->save_award_info($this->mid, $this->request_params);
        } catch (Exception $e) {
            $this->result = array('status' => false, 'msg' => $e->getMessage());
        }
    }
}